﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Character;
using Studio;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace StudioFileCheck
{
	// Token: 0x02000846 RID: 2118
	public class StudioFileCheck : MonoBehaviour
	{
		// Token: 0x06003106 RID: 12550 RVA: 0x000FF234 File Offset: 0x000FD434
		private void MoveFile(string _directory, string _file)
		{
			string sourceFileName = UserData.Create("CheckStudio") + _file;
			string destFileName = UserData.Create("CheckStudio/" + _directory) + _file;
			try
			{
				File.Move(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		// Token: 0x06003107 RID: 12551 RVA: 0x000FF28C File Offset: 0x000FD48C
		private void OnSelect(int _idx)
		{
			StudioFileCheck.ModCheckInfo modCheckInfo = this.listModCheck[_idx];
			this.fileName = modCheckInfo.file;
			this.textFile.text = this.fileName;
			this.DeleteInfoNode();
			if (!modCheckInfo.isLoad)
			{
				this.AddNode(this.fileInfo.objectNode, this.fileInfo.transformRoot, "読み込めない");
				return;
			}
			if (modCheckInfo.isMod)
			{
				this.AddNode(this.fileInfo.objectNode, this.fileInfo.transformRoot, "Mod情報");
                foreach (UniRx.Tuple<OICharInfo, StudioFileCheck.ModInfo> tuple in modCheckInfo.list)
                {
                    this.AddNode(this.fileInfo.objectNode, this.fileInfo.transformRoot, tuple.Item2.text);
                }
            }
			if (modCheckInfo.info.dicObject.Count != 0)
			{
				List<string> list = new List<string>();
				this.GetInfoLoop(modCheckInfo.info.dicObject.Values.ToList<ObjectInfo>(), list, 0);
				foreach (string text in list)
				{
					this.AddNode(this.fileInfo.objectNode, this.fileInfo.transformRoot, text);
				}
			}
			else
			{
				this.AddNode(this.fileInfo.objectNode, this.fileInfo.transformRoot, "キャラ、アイテム等が存在しない");
			}
			this.fileInfo.scrollRect.normalizedPosition = Vector2.one;
		}

		// Token: 0x06003108 RID: 12552 RVA: 0x000FF46C File Offset: 0x000FD66C
		private void DeleteInfoNode()
		{
			for (int i = 0; i < this.fileInfo.transformRoot.childCount; i++)
			{
				UnityEngine.Object.Destroy(this.fileInfo.transformRoot.GetChild(i).gameObject);
			}
			this.fileInfo.transformRoot.DetachChildren();
		}

		// Token: 0x06003109 RID: 12553 RVA: 0x000FF4C8 File Offset: 0x000FD6C8
		private StudioNode AddNode(GameObject _node, Transform _root, string _text)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(_node);
			gameObject.transform.SetParent(_root, false);
			StudioNode component = gameObject.GetComponent<StudioNode>();
			component.active = true;
			component.text = _text;
			return component;
		}

		// Token: 0x0600310A RID: 12554 RVA: 0x000FF500 File Offset: 0x000FD700
		private void AddNode(GameObject _node, Transform _root, StudioFileCheck.ModCheckInfo _info, int _idx)
		{
			StudioNode studioNode = this.AddNode(_node, _root, _info.file);
			studioNode.buttonUI.OnClickAsObservable().Subscribe(delegate(Unit _)
			{
				this.OnSelect(_idx);
			});
			studioNode.textColor = ((!_info.isLoad) ? Color.yellow : ((!_info.isMod) ? Color.white : Color.red));
		}

		// Token: 0x0600310B RID: 12555 RVA: 0x000FF584 File Offset: 0x000FD784
		private void GetInfoLoop(List<ObjectInfo> _infoList, List<string> _list, int _indent)
		{
			string text = string.Empty;
			for (int i = 0; i < _indent; i++)
			{
				text += "\t";
			}
			foreach (ObjectInfo objectInfo in _infoList)
			{
				int kind = objectInfo.kind;
				if (kind != 0)
				{
					if (kind != 1)
					{
						if (kind == 3)
						{
							OIFolderInfo oifolderInfo = objectInfo as OIFolderInfo;
							_list.Add(string.Format("{1}F : {0}", oifolderInfo.name, text));
							this.GetInfoLoop(oifolderInfo.child, _list, _indent + 1);
						}
					}
					else
					{
						OIItemInfo oiitemInfo = objectInfo as OIItemInfo;
						if (Singleton<Info>.IsInstance())
						{
							Info.ItemLoadInfo itemLoadInfo = null;
							_list.Add(string.Format("{1}I : {0}", (!Singleton<Info>.Instance.dicItemLoadInfo.TryGetValue(oiitemInfo.no, out itemLoadInfo)) ? "存在しない" : itemLoadInfo.name, text));
						}
						else
						{
							_list.Add(string.Format("{1}I : {0}", oiitemInfo.no, text));
						}
						this.GetInfoLoop(oiitemInfo.child, _list, _indent + 1);
					}
				}
				else
				{
					OICharInfo oicharInfo = objectInfo as OICharInfo;
					_list.Add(string.Format("{1}C : {0}", oicharInfo.fileStatus.name, text));
					foreach (KeyValuePair<int, List<ObjectInfo>> keyValuePair in oicharInfo.child)
					{
						this.GetInfoLoop(keyValuePair.Value, _list, _indent + 1);
					}
				}
			}
		}

		// Token: 0x0600310C RID: 12556 RVA: 0x000FF778 File Offset: 0x000FD978
		private StudioFileCheck.ModInfo Check(OICharInfo _info)
		{
			if (_info.charFile.Sex != SEX.FEMALE && _info.charFile.Sex != SEX.MALE)
			{
				return new StudioFileCheck.ModInfo(0, "性別詐称");
			}
			if (_info.charFile.acce.slot.Length != 10)
			{
				return new StudioFileCheck.ModInfo(1, "アクセサリースロット偽造");
			}
			if (_info.charFile.head.shapeVals.Any((float f) => !MathfEx.RangeEqualOn<float>(0f, f, 1f)))
			{
				return new StudioFileCheck.ModInfo(2, string.Format("カスタム値の範囲外 : {0}", "顔"));
			}
			if (_info.charFile.body.shapeVals.Any((float f) => !MathfEx.RangeEqualOn<float>(0f, f, 1f)))
			{
				return new StudioFileCheck.ModInfo(3, string.Format("カスタム値の範囲外 : {0}", "体"));
			}
			if (_info.charFile.Sex == SEX.FEMALE)
			{
				if (!MathfEx.RangeEqualOn<float>(0f, _info.charFile.body.areolaSize, 1f))
				{
					return new StudioFileCheck.ModInfo(4, string.Format("{0}", "乳輪の大きさ"));
				}
				if (!MathfEx.RangeEqualOn<float>(0f, _info.charFile.body.bustSoftness, 1f))
				{
					return new StudioFileCheck.ModInfo(5, string.Format("{0}", "0.5f"));
				}
				if (!MathfEx.RangeEqualOn<float>(0f, _info.charFile.body.bustWeight, 1f))
				{
					return new StudioFileCheck.ModInfo(6, string.Format("{0}", "0.5f"));
				}
			}
			var list = (from v in _info.charFile.acce.slot.Select((AccessoryCustom v, int i) => new
			{
				v,
				i
			})
			where v.v.type != ACCESSORY_TYPE.NONE
			where !CustomDataManager.HasAcceData(v.v.type, v.v.id)
			select v).ToList();
			if (!list.IsNullOrEmpty())
			{
				StringBuilder stringBuilder = new StringBuilder();
				foreach (var callback in list)
				{
					stringBuilder.Append(string.Format("アクセサリータイプ[{0}] : ID[{1}], ", callback.v.type, callback.v.id));
				}
				return new StudioFileCheck.ModInfo(7, stringBuilder.ToString());
			}
			List<WearCustom> list2 = (from v in _info.charFile.wear.wears
			where v.id != -1
			where !CustomDataManager.HasWearData(_info.charFile.Sex, v.type, v.id)
			select v).ToList<WearCustom>();
			if (!list2.IsNullOrEmpty<WearCustom>())
			{
				StringBuilder stringBuilder2 = new StringBuilder();
				foreach (WearCustom wearCustom in list2)
				{
					stringBuilder2.Append(string.Format("服タイプ[{0}] : ID[{1}], ", wearCustom.type, wearCustom.id));
				}
				return new StudioFileCheck.ModInfo(8, stringBuilder2.ToString());
			}
			return null;
		}

		// Token: 0x17000557 RID: 1367
		// (set) Token: 0x0600310D RID: 12557 RVA: 0x000FFB5C File Offset: 0x000FDD5C
		private float nextFrameTime
		{
			set
			{
				this.m_NextFrameTime = Time.realtimeSinceStartup + value;
			}
		}

		// Token: 0x17000558 RID: 1368
		// (get) Token: 0x0600310E RID: 12558 RVA: 0x000FFB6C File Offset: 0x000FDD6C
		private bool isOver
		{
			get
			{
				return Time.realtimeSinceStartup >= this.m_NextFrameTime;
			}
		}

		// Token: 0x0600310F RID: 12559 RVA: 0x000FFB80 File Offset: 0x000FDD80
		private IEnumerator ModCheckCoroutine()
		{
			this.nextFrameTime = 0.01f;
			this.canvasOver.enabled = true;
			this.graphicRaycasterOver.enabled = true;
			for (int k = 0; k < this.fileList.transformRoot.childCount; k++)
			{
				UnityEngine.Object.Destroy(this.fileList.transformRoot.GetChild(k).gameObject);
			}
			this.fileList.transformRoot.DetachChildren();
			if (this.isOver)
			{
				yield return null;
				this.nextFrameTime = 0.01f;
			}
			this.DeleteInfoNode();
			if (this.isOver)
			{
				yield return null;
				this.nextFrameTime = 0.01f;
			}
			string[] files = Directory.GetFiles(UserData.Create("CheckStudio"), "*.png");
			if (this.isOver)
			{
				yield return null;
				this.nextFrameTime = 0.01f;
			}
			this.listModCheck = new List<StudioFileCheck.ModCheckInfo>();
			foreach (string s in files)
			{
				if (this.isOver)
				{
					yield return null;
					this.nextFrameTime = 0.01f;
				}
				StudioFileCheck.ModCheckInfo info = new StudioFileCheck.ModCheckInfo();
				info.file = Path.GetFileName(s);
				info.info = new SceneInfo(null);
				info.isLoad = info.info.Load(s, out info.version);
				if (this.isOver)
				{
					yield return null;
					this.nextFrameTime = 0.01f;
				}
				if (!info.isLoad)
				{
					this.listModCheck.Add(info);
				}
				else if (info.info.dicObject.Count<KeyValuePair<int, ObjectInfo>>() == 0)
				{
					this.listModCheck.Add(info);
				}
				else
				{
					List<OICharInfo> targets = info.info.dicObject.Values.OfType<OICharInfo>().ToList<OICharInfo>();
					if (targets.IsNullOrEmpty<OICharInfo>())
					{
						this.listModCheck.Add(info);
					}
					else
					{
						if (this.isOver)
						{
							yield return null;
							this.nextFrameTime = 0.01f;
						}
                        //info.list = (from v in targets
                        //             select new UniRx.Tuple<OICharInfo, StudioFileCheck.ModInfo>(v, this.Check(v)) into v
                        //             where v.Item2 != null
                        //             select v).ToList<Tuple<OICharInfo, StudioFileCheck.ModInfo>>();
                        //this.listModCheck.Add(info);
                    }
				}
			}
			foreach (StudioFileCheck.ModCheckInfo v4 in from i in this.listModCheck
			where i.isMod
			select i)
			{
				if (this.isOver)
				{
					yield return null;
					this.nextFrameTime = 0.01f;
				}
				this.MoveFile("MOD", v4.file);
			}
			foreach (StudioFileCheck.ModCheckInfo v2 in from i in this.listModCheck
			where !i.isLoad
			select i)
			{
				if (this.isOver)
				{
					yield return null;
					this.nextFrameTime = 0.01f;
				}
				this.MoveFile("Unknown", v2.file);
			}
			foreach (var v3 in this.listModCheck.Select((StudioFileCheck.ModCheckInfo v, int i) => new
			{
				v,
				i
			}))
			{
				if (this.isOver)
				{
					yield return null;
					this.nextFrameTime = 0.01f;
				}
				this.AddNode(this.fileList.objectNode, this.fileList.transformRoot, v3.v, v3.i);
			}
			this.canvasOver.enabled = false;
			this.graphicRaycasterOver.enabled = false;
			yield break;
		}

		// Token: 0x06003110 RID: 12560 RVA: 0x000FFB9C File Offset: 0x000FDD9C
		private IEnumerator Start()
		{
			if (!Singleton<Info>.Instance.isLoadList)
			{
				//Singleton<Studio.Scene>.Instance.Load(new Studio.Scene.Data
				//{
				//	sceneName = "StudioWait",
				//	isAdd = true,
				//	isLoading = false,
				//	isAsync = false,
				//	isFade = false,
				//	isOverlap = false
				//});
				yield return Singleton<Info>.Instance.LoadExcelDataCoroutine();
				Singleton<Studio.Scene>.Instance.UnLoad();
			}
			this.buttonModCheck.OnClickAsObservable().Subscribe(delegate(Unit _)
			{
				Observable.FromMicroCoroutine(new Func<IEnumerator>(this.ModCheckCoroutine), false, FrameCountType.Update).Subscribe<Unit>();
			});
			CustomDataManager.Setup();
			yield break;
		}

		// Token: 0x040030D7 RID: 12503
		private const string dirName = "CheckStudio";

		// Token: 0x040030D8 RID: 12504
		[SerializeField]
		private Button buttonModCheck;

		// Token: 0x040030D9 RID: 12505
		[SerializeField]
		private StudioFileCheck.ScrollViewInfo fileList = new StudioFileCheck.ScrollViewInfo();

		// Token: 0x040030DA RID: 12506
		[SerializeField]
		private Text textFile;

		// Token: 0x040030DB RID: 12507
		[SerializeField]
		private StudioFileCheck.ScrollViewInfo fileInfo = new StudioFileCheck.ScrollViewInfo();

		// Token: 0x040030DC RID: 12508
		[SerializeField]
		private Canvas canvasOver;

		// Token: 0x040030DD RID: 12509
		[SerializeField]
		private GraphicRaycaster graphicRaycasterOver;

		// Token: 0x040030DE RID: 12510
		private List<StudioFileCheck.ModCheckInfo> listModCheck;

		// Token: 0x040030DF RID: 12511
		private string fileName = string.Empty;

		// Token: 0x040030E0 RID: 12512
		private float m_NextFrameTime;

		// Token: 0x02000847 RID: 2119
		[Serializable]
		private class ScrollViewInfo
		{
			// Token: 0x040030E7 RID: 12519
			public GameObject objectNode;

			// Token: 0x040030E8 RID: 12520
			public Transform transformRoot;

			// Token: 0x040030E9 RID: 12521
			public ScrollRect scrollRect;
		}

		// Token: 0x02000848 RID: 2120
		private class ModInfo
		{
			// Token: 0x06003118 RID: 12568 RVA: 0x000FFC40 File Offset: 0x000FDE40
			public ModInfo(int _type, string _text)
			{
				this.type = _type;
				this.text = _text;
			}

			// Token: 0x17000559 RID: 1369
			// (get) Token: 0x06003119 RID: 12569 RVA: 0x000FFC58 File Offset: 0x000FDE58
			// (set) Token: 0x0600311A RID: 12570 RVA: 0x000FFC60 File Offset: 0x000FDE60
			public int type { get; private set; }

			// Token: 0x1700055A RID: 1370
			// (get) Token: 0x0600311B RID: 12571 RVA: 0x000FFC6C File Offset: 0x000FDE6C
			// (set) Token: 0x0600311C RID: 12572 RVA: 0x000FFC74 File Offset: 0x000FDE74
			public string text { get; private set; }
		}

		// Token: 0x02000849 RID: 2121
		private class ModCheckInfo
		{
			// Token: 0x1700055B RID: 1371
			// (get) Token: 0x0600311E RID: 12574 RVA: 0x000FFC94 File Offset: 0x000FDE94
			public bool isMod
			{
				get
				{
					//return !this.list.IsNullOrEmpty<Tuple<OICharInfo, StudioFileCheck.ModInfo>>();
					return false;
				}
			}

			// Token: 0x040030EC RID: 12524
			public string file = string.Empty;

			// Token: 0x040030ED RID: 12525
			public Version version;

			// Token: 0x040030EE RID: 12526
			public bool isLoad;

			// Token: 0x040030EF RID: 12527
			public List<UniRx.Tuple<OICharInfo, StudioFileCheck.ModInfo>> list;

			// Token: 0x040030F0 RID: 12528
			public SceneInfo info;
		}
	}
}
